# -*- coding: utf-8 -*-
"""
    Created by 杜神 at 2018/3/28
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import MySQLdb
import logging

logging.basicConfig(
    level=logging.DEBUG,
    format=
    '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    datefmt='%a, %d %b %Y %H:%M:%S',
    filename='loaddata.log',
    filemode='w')

if len(sys.argv) < 2:
    print 'sys.argv:', len(sys.argv)
    print '缺少参数!'
    print  'usage:'
    print 'python loadmoviedata.py [moviename]'
    print 'exit(1)'
    exit(1)
#moviename = sys.argv[1]
moviename = '斗破苍穹第二季'
print '视频名称：' + moviename
conn = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='ROOT', db='movie-website1', charset='utf8')
cur = conn.cursor()
logging.info('第一步：')
sql = "select * from spider_h6080_movieinfo WHERE moviename like '%"+moviename+"%'"
cur.execute(sql)
if not len(cur.fetchall()) == 1:
    logging.info('该视频名关键字查询结果不唯一')
    logging.info('第一步中断！')
    logging.info('exit(1)')
    exit(1)
logging.info(sql)
logging.info(cur.fetchall())
logging.info('第二步：')
sql = "INSERT INTO website1_movie ( \
                moviename,\
                prefilename,\
                suffixname,\
                createtime,\
                updatetime,\
                publishtime,\
                TYPES,\
                AREA,\
                LANGUAGE,\
                actor,\
                director,\
                keyword,\
                weight,\
                countnumber,\
                avaliblesum,\
                introduce,\
                clickcount,\
                playcount,\
                duration,\
                isoutsource,\
                picurl,\
                classify_id\
                )\
                SELECT\
                moviename,\
                '',\
                '',\
                '2018-03-28',\
                '2018-03-28',\
                publishtime,\
                TYPES,\
                AREA,\
                '国语',\
                actor,\
                director,\
                '"+moviename+"',\
                5,\
                1,\
                1,\
                introduce,\
                0,\
                0,\
                '60分钟',\
                1,\
                picurl,\
                2\
                FROM\
                spider_h6080_movieinfo WHERE `moviename` LIKE '%"+moviename+"%'"
logging.info(sql)
try:
    cur.execute(sql)
    conn.commit()
except:
    logging.info('第二步中断！')
    logging.info(sql)
    logging.info('exit(1)')
    exit(1)
logging.info('第三步：')
cur.execute("SELECT id FROM website1_movie t WHERE t.moviename LIKE '%"+moviename+"%'")
id = cur.fetchall()[0][0]
logging.info('该视频信息id为:%s' % id)
logging.info(cur.fetchall())
logging.info('第四步：')
sql = "INSERT INTO website1_outsource (num, outfilelink, movie_id)\
    SELECT\
    num,\
    url,\
    " + str(id) + "	\
    from \
    spider_h6080 WHERE NAME LIKE '%"+moviename+"%' ORDER BY CAST(num AS SIGNED) ASC"
logging.info(sql)
try:
    cur.execute(sql)
    conn.commit()
except:
    logging.info('第四步中断！')
    logging.info(sql)
    logging.info('exit(1)')
    exit(1)
logging.info("第五步：")
sql = "SELECT * FROM website1_outsource WHERE movie_id ='"+str(id)+"' \
    AND id%2=0 ORDER BY CAST(num AS SIGNED) ASC"
cur.execute(sql)
logging.info(sql)
logging.info(cur.fetchall())
#logging.info("第六步：")
#sql = "DELETE FROM website1_outsource WHERE movie_id='"+str(id)+"' AND id%2=0;"
#logging.info(sql)
#try:
#    cur.execute(sql)
#    conn.commit()
#except:
#    logging.info('第六步中断！')
#    logging.info(sql)
#    logging.info('exit(1)')
#    exit(1)
logging.info("操作完成，程序终止！")
logging.info("======================================================================================")